
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.1 线程的基本操作 · C++并发编程实战（第2版）</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="陈晓伟 chenxiaowei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="2.2-chinese.html" />
    
    
    <link rel="prev" href="2.0-chinese.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../Praise_for_the_first_edition.html">
            
                <a href="../Praise_for_the_first_edition.html">
            
                    
                    第一版的赞许
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../preface-chinese.html">
            
                <a href="../preface-chinese.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../acknowledgments-chinese.html">
            
                <a href="../acknowledgments-chinese.html">
            
                    
                    感谢
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../about_this_book-chinese.html">
            
                <a href="../about_this_book-chinese.html">
            
                    
                    关于本书
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../about_the_author-chinese.html">
            
                <a href="../about_the_author-chinese.html">
            
                    
                    关于作者
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../about_cover_illustration-chinese.html">
            
                <a href="../about_cover_illustration-chinese.html">
            
                    
                    关于封面
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter1/1.0-chinese.html">
            
                <a href="../chapter1/1.0-chinese.html">
            
                    
                    第1章 你好，并发世界
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter1/1.1-chinese.html">
            
                <a href="../chapter1/1.1-chinese.html">
            
                    
                    1.1 何谓并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter1/1.2-chinese.html">
            
                <a href="../chapter1/1.2-chinese.html">
            
                    
                    1.2 为什么使用并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter1/1.3-chinese.html">
            
                <a href="../chapter1/1.3-chinese.html">
            
                    
                    1.3 并发和多线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter1/1.4-chinese.html">
            
                <a href="../chapter1/1.4-chinese.html">
            
                    
                    1.4 开始入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter1/1.5-chinese.html">
            
                <a href="../chapter1/1.5-chinese.html">
            
                    
                    1.5 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="2.0-chinese.html">
            
                <a href="2.0-chinese.html">
            
                    
                    第2章 线程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.9.1" data-path="2.1-chinese.html">
            
                <a href="2.1-chinese.html">
            
                    
                    2.1 线程的基本操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="2.2-chinese.html">
            
                <a href="2.2-chinese.html">
            
                    
                    2.2 传递参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="2.3-chinese.html">
            
                <a href="2.3-chinese.html">
            
                    
                    2.3 转移所有权
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="2.4-chinese.html">
            
                <a href="2.4-chinese.html">
            
                    
                    2.4 确定线程数量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="2.5-chinese.html">
            
                <a href="2.5-chinese.html">
            
                    
                    2.5 线程标识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="2.6-chinese.html">
            
                <a href="2.6-chinese.html">
            
                    
                    2.6 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter3/3.0-chinese.html">
            
                <a href="../chapter3/3.0-chinese.html">
            
                    
                    第3章 共享数据
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter3/3.1-chinese.html">
            
                <a href="../chapter3/3.1-chinese.html">
            
                    
                    3.1 共享数据的问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter3/3.2-chinese.html">
            
                <a href="../chapter3/3.2-chinese.html">
            
                    
                    3.2 使用互斥量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter3/3.3-chinese.html">
            
                <a href="../chapter3/3.3-chinese.html">
            
                    
                    3.3 保护共享数据的方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter3/3.4-chinese.html">
            
                <a href="../chapter3/3.4-chinese.html">
            
                    
                    3.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter4/4.0-chinese.html">
            
                <a href="../chapter4/4.0-chinese.html">
            
                    
                    第4章 同步操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter4/4.1-chinese.html">
            
                <a href="../chapter4/4.1-chinese.html">
            
                    
                    4.1 等待事件或条件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter4/4.2-chinese.html">
            
                <a href="../chapter4/4.2-chinese.html">
            
                    
                    4.2 使用future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter4/4.3-chinese.html">
            
                <a href="../chapter4/4.3-chinese.html">
            
                    
                    4.3 限时等待
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter4/4.4-chinese.html">
            
                <a href="../chapter4/4.4-chinese.html">
            
                    
                    4.4 简化代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter4/4.5-chinese.html">
            
                <a href="../chapter4/4.5-chinese.html">
            
                    
                    4.5 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter5/5.0-chinese.html">
            
                <a href="../chapter5/5.0-chinese.html">
            
                    
                    第5章 内存模型和原子操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../chapter5/5.1-chinese.html">
            
                <a href="../chapter5/5.1-chinese.html">
            
                    
                    5.1 内存模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../chapter5/5.2-chinese.html">
            
                <a href="../chapter5/5.2-chinese.html">
            
                    
                    5.2 原子操作和原子类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../chapter5/5.3-chinese.html">
            
                <a href="../chapter5/5.3-chinese.html">
            
                    
                    5.3 同步操作和强制排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../chapter5/5.4-chinese.html">
            
                <a href="../chapter5/5.4-chinese.html">
            
                    
                    5.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../chapter6/6.0-chinese.html">
            
                <a href="../chapter6/6.0-chinese.html">
            
                    
                    第6章 设计基于锁的并发数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../chapter6/6.1-chinese.html">
            
                <a href="../chapter6/6.1-chinese.html">
            
                    
                    6.1 并发设计的意义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../chapter6/6.2-chinese.html">
            
                <a href="../chapter6/6.2-chinese.html">
            
                    
                    6.2 基于锁的并发数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../chapter6/6.3-chinese.html">
            
                <a href="../chapter6/6.3-chinese.html">
            
                    
                    6.3 设计更加复杂的数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../chapter6/6.4-chinese.html">
            
                <a href="../chapter6/6.4-chinese.html">
            
                    
                    6.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../chapter7/7.0-chinese.html">
            
                <a href="../chapter7/7.0-chinese.html">
            
                    
                    第7章 设计无锁的并发数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../chapter7/7.1-chinese.html">
            
                <a href="../chapter7/7.1-chinese.html">
            
                    
                    7.1 定义和意义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../chapter7/7.2-chinese.html">
            
                <a href="../chapter7/7.2-chinese.html">
            
                    
                    7.2 无锁数据结构的例子
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../chapter7/7.3-chinese.html">
            
                <a href="../chapter7/7.3-chinese.html">
            
                    
                    7.3 设计无锁数据结构的指导建议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../chapter7/7.4-chinese.html">
            
                <a href="../chapter7/7.4-chinese.html">
            
                    
                    7.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../chapter8/8.0-chinese.html">
            
                <a href="../chapter8/8.0-chinese.html">
            
                    
                    第8章 并发设计
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../chapter8/8.1-chinese.html">
            
                <a href="../chapter8/8.1-chinese.html">
            
                    
                    8.1 线程间划分工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../chapter8/8.2-chinese.html">
            
                <a href="../chapter8/8.2-chinese.html">
            
                    
                    8.2 并发代码的性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../chapter8/8.3-chinese.html">
            
                <a href="../chapter8/8.3-chinese.html">
            
                    
                    8.3 为多线程性能设计数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.4" data-path="../chapter8/8.4-chinese.html">
            
                <a href="../chapter8/8.4-chinese.html">
            
                    
                    8.4 设计并发代码的注意事项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.5" data-path="../chapter8/8.5-chinese.html">
            
                <a href="../chapter8/8.5-chinese.html">
            
                    
                    8.5 在实践中设计并发代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.6" data-path="../chapter8/8.6-chinese.html">
            
                <a href="../chapter8/8.6-chinese.html">
            
                    
                    8.6 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../chapter9/9.0-chinese.html">
            
                <a href="../chapter9/9.0-chinese.html">
            
                    
                    第9章 高级线程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.16.1" data-path="../chapter9/9.1-chinese.html">
            
                <a href="../chapter9/9.1-chinese.html">
            
                    
                    9.1 线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="../chapter9/9.2-chinese.html">
            
                <a href="../chapter9/9.2-chinese.html">
            
                    
                    9.2 中断线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="../chapter9/9.3-chinese.html">
            
                <a href="../chapter9/9.3-chinese.html">
            
                    
                    9.3 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../chapter10/10.0-chinese.html">
            
                <a href="../chapter10/10.0-chinese.html">
            
                    
                    第10章 并行算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../chapter10/10.1-chinese.html">
            
                <a href="../chapter10/10.1-chinese.html">
            
                    
                    10.1 并行化标准库算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../chapter10/10.2-chinese.html">
            
                <a href="../chapter10/10.2-chinese.html">
            
                    
                    10.2 执行策略
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../chapter10/10.3-chinese.html">
            
                <a href="../chapter10/10.3-chinese.html">
            
                    
                    10.3 C++标准库中的并行算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../chapter10/10.4-chinese.html">
            
                <a href="../chapter10/10.4-chinese.html">
            
                    
                    10.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../chapter11/11.0-chinese.html">
            
                <a href="../chapter11/11.0-chinese.html">
            
                    
                    第11章 测试和调试多线程应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../chapter11/11.1-chinese.html">
            
                <a href="../chapter11/11.1-chinese.html">
            
                    
                    11.1 与并发相关的Bug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../chapter11/11.2-chinese.html">
            
                <a href="../chapter11/11.2-chinese.html">
            
                    
                    11.2 定位并发Bug的技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../chapter11/11.3-chinese.html">
            
                <a href="../chapter11/11.3-chinese.html">
            
                    
                    11.3 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../appendix_A/A.0-chinese.html">
            
                <a href="../appendix_A/A.0-chinese.html">
            
                    
                    附录A C++11特性简明参考(部分)
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../appendix_A/A.1-chinese.html">
            
                <a href="../appendix_A/A.1-chinese.html">
            
                    
                    A.1 右值引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.2" data-path="../appendix_A/A.2-chinese.html">
            
                <a href="../appendix_A/A.2-chinese.html">
            
                    
                    A.2 删除函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.3" data-path="../appendix_A/A.3-chinese.html">
            
                <a href="../appendix_A/A.3-chinese.html">
            
                    
                    A.3 默认函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.4" data-path="../appendix_A/A.4-chinese.html">
            
                <a href="../appendix_A/A.4-chinese.html">
            
                    
                    A.4 常量表达式函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.5" data-path="../appendix_A/A.5-chinese.html">
            
                <a href="../appendix_A/A.5-chinese.html">
            
                    
                    A.5 Lambda函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.6" data-path="../appendix_A/A.6-chinese.html">
            
                <a href="../appendix_A/A.6-chinese.html">
            
                    
                    A.6 变参模板
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.7" data-path="../appendix_A/A.7-chinese.html">
            
                <a href="../appendix_A/A.7-chinese.html">
            
                    
                    A.7 自动推导变量类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.8" data-path="../appendix_A/A.8-chinese.html">
            
                <a href="../appendix_A/A.8-chinese.html">
            
                    
                    A.8 线程本地变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.9" data-path="../appendix_A/A.9-chinese.html">
            
                <a href="../appendix_A/A.9-chinese.html">
            
                    
                    A.9 模板类参数的推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.10" data-path="../appendix_A/A.10-chinese.html">
            
                <a href="../appendix_A/A.10-chinese.html">
            
                    
                    A.10 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../appendix_B/B.0-chinese.html">
            
                <a href="../appendix_B/B.0-chinese.html">
            
                    
                    附录B 并发库简要对比
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../appendix_C/C.0-chinese.html">
            
                <a href="../appendix_C/C.0-chinese.html">
            
                    
                    附录C 消息传递框架与完整的ATM示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../appendix_D/D.0-chinese.html">
            
                <a href="../appendix_D/D.0-chinese.html">
            
                    
                    附录D C++线程类库参考
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.22.1" data-path="../appendix_D/D.1-chinese.html">
            
                <a href="../appendix_D/D.1-chinese.html">
            
                    
                    D.1 chrono头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.2" data-path="../appendix_D/D.2-chinese.html">
            
                <a href="../appendix_D/D.2-chinese.html">
            
                    
                    D.2 condition_variable头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.3" data-path="../appendix_D/D.3-chinese.html">
            
                <a href="../appendix_D/D.3-chinese.html">
            
                    
                    D.3 atomic头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.4" data-path="../appendix_D/D.4-chinese.html">
            
                <a href="../appendix_D/D.4-chinese.html">
            
                    
                    D.4 future头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.5" data-path="../appendix_D/D.5-chinese.html">
            
                <a href="../appendix_D/D.5-chinese.html">
            
                    
                    D.5 mutex头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.6" data-path="../appendix_D/D.6-chinese.html">
            
                <a href="../appendix_D/D.6-chinese.html">
            
                    
                    D.6 ratio头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.7" data-path="../appendix_D/D.7-chinese.html">
            
                <a href="../appendix_D/D.7-chinese.html">
            
                    
                    D.7 thread头文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >2.1 线程的基本操作</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="21-&#x7EBF;&#x7A0B;&#x7684;&#x57FA;&#x672C;&#x64CD;&#x4F5C;">2.1 &#x7EBF;&#x7A0B;&#x7684;&#x57FA;&#x672C;&#x64CD;&#x4F5C;</h1>
<p>&#x6BCF;&#x4E2A;&#x7A0B;&#x5E8F;&#x81F3;&#x5C11;&#x6709;&#x4E00;&#x4E2A;&#x6267;&#x884C;main()&#x51FD;&#x6570;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x4E0E;&#x4E3B;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x8FD0;&#x884C;&#x3002;&#x5982;main()&#x51FD;&#x6570;&#x6267;&#x884C;&#x5B8C;&#x4F1A;&#x9000;&#x51FA;&#x4E00;&#x6837;&#xFF0C;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x51FD;&#x6570;&#x4E5F;&#x4F1A;&#x9000;&#x51FA;&#x3002;&#x4E3A;&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;<code>std::thread</code>&#x5BF9;&#x8C61;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x8FD9;&#x4E2A;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#x3002;&#x90A3;&#x4E48;&#xFF0C;&#x5C31;&#x5148;&#x6765;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#x3002;</p>
<h2 id="211-&#x542F;&#x52A8;&#x7EBF;&#x7A0B;">2.1.1 &#x542F;&#x52A8;&#x7EBF;&#x7A0B;</h2>
<p>&#x7B2C;1&#x7AE0;&#x4E2D;&#xFF0C;&#x7EBF;&#x7A0B;&#x5728;<code>std::thread</code>&#x5BF9;&#x8C61;&#x521B;&#x5EFA;&#x65F6;&#x542F;&#x52A8;&#xFF0C;&#x901A;&#x5E38;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x65E0;&#x53C2;&#x6570;&#x65E0;&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x3002;&#x8FD9;&#x79CD;&#x51FD;&#x6570;&#x5728;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#xFF0C;&#x7EBF;&#x7A0B;&#x4E5F;&#x5C31;&#x7ED3;&#x675F;&#x4E86;&#x3002;&#x4E00;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4EFB;&#x52A1;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x67D0;&#x79CD;&#x901A;&#x8BAF;&#x673A;&#x5236;&#x8FDB;&#x884C;&#x53C2;&#x6570;&#x7684;&#x4F20;&#x9012;&#xFF0C;&#x6216;&#x8005;&#x6267;&#x884C;&#x4E00;&#x7CFB;&#x5217;&#x72EC;&#x7ACB;&#x64CD;&#x4F5C;&#xFF0C;&#x901A;&#x8FC7;&#x901A;&#x8BAF;&#x673A;&#x5236;&#x4F20;&#x9012;&#x4FE1;&#x53F7;&#x8BA9;&#x7EBF;&#x7A0B;&#x505C;&#x6B62;&#x3002;&#x5148;&#x653E;&#x4E0B;&#x8FD9;&#x4E9B;&#x7279;&#x6B8A;&#x60C5;&#x51B5;&#x4E0D;&#x8C08;&#xFF0C;&#x7B80;&#x5355;&#x6765;&#x8BF4;&#xFF0C;&#x4F7F;&#x7528;C++&#x7EBF;&#x7A0B;&#x5E93;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#xFF0C;&#x5C31;&#x662F;&#x6784;&#x9020;<code>std::thread</code>&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">do_some_work</span><span class="hljs-params">()</span></span>;
<span class="hljs-built_in">std</span>::<span class="hljs-function">thread <span class="hljs-title">my_thread</span><span class="hljs-params">(do_some_work)</span></span>;
</code></pre>
<p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x5305;&#x542B;<code>&lt;thread&gt;</code>&#x5934;&#x6587;&#x4EF6;&#xFF0C;<code>std::thread</code>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6709;&#x51FD;&#x6570;&#x64CD;&#x4F5C;&#x7B26;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x8FDB;&#x884C;&#x6784;&#x9020;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> background_task
{
<span class="hljs-keyword">public</span>:
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span>
  </span>{
    do_something();
    do_something_else();
  }
};

background_task f;
<span class="hljs-built_in">std</span>::<span class="hljs-function">thread <span class="hljs-title">my_thread</span><span class="hljs-params">(f)</span></span>;
</code></pre>
<p>&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x63D0;&#x4F9B;&#x7684;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x4F1A;&#x590D;&#x5236;&#x5230;&#x65B0;&#x7EBF;&#x7A0B;&#x7684;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#x4E2D;&#xFF0C;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x7684;&#x6267;&#x884C;&#x548C;&#x8C03;&#x7528;&#x90FD;&#x5728;&#x7EBF;&#x7A0B;&#x7684;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x4E2D;&#x8FDB;&#x884C;&#x3002;</p>
<p>&#x6709;&#x4EF6;&#x4E8B;&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x5F53;&#x628A;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x4F20;&#x5165;&#x5230;&#x7EBF;&#x7A0B;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x907F;&#x514D;&#x201C;<a href="http://en.wikipedia.org/wiki/Most_vexing_parse" target="_blank">&#x6700;&#x4EE4;&#x4EBA;&#x5934;&#x75DB;&#x7684;&#x8BED;&#x6CD5;&#x89E3;&#x6790;</a>&#x201D;(<em>C++&#x2019;s most vexing parse</em>, <a href="http://qiezhuifeng.diandian.com/post/2012-08-27/40038339477" target="_blank">&#x4E2D;&#x6587;&#x7B80;&#x4ECB;</a>)&#x3002;&#x5982;&#x679C;&#x4F60;&#x4F20;&#x9012;&#x4E86;&#x4E00;&#x4E2A;&#x4E34;&#x65F6;&#x53D8;&#x91CF;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x7684;&#x53D8;&#x91CF;&#x3002;C++&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x5C06;&#x5176;&#x89E3;&#x6790;&#x4E3A;&#x51FD;&#x6570;&#x58F0;&#x660E;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7C7B;&#x578B;&#x5BF9;&#x8C61;&#x7684;&#x5B9A;&#x4E49;&#x3002;</p>
<pre><code>std::thread my_thread(background_task());
</code></pre><p>&#x8FD9;&#x76F8;&#x5F53;&#x4E8E;&#x58F0;&#x660E;&#x4E86;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;my_thread&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5E26;&#x6709;&#x4E00;&#x4E2A;&#x53C2;&#x6570;(&#x51FD;&#x6570;&#x6307;&#x9488;&#x6307;&#x5411;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#x5E76;&#x8FD4;&#x56DE;background_task&#x5BF9;&#x8C61;&#x7684;&#x51FD;&#x6570;)&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>std::thread</code>&#x5BF9;&#x8C61;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x5728;&#x524D;&#x9762;&#x547D;&#x540D;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x6216;&#x4F7F;&#x7528;&#x591A;&#x7EC4;&#x62EC;&#x53F7;&#x2460;&#xFF0C;&#x6216;&#x4F7F;&#x7528;&#x7EDF;&#x4E00;&#x7684;&#x521D;&#x59CB;&#x5316;&#x8BED;&#x6CD5;&#x2461;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x907F;&#x514D;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-c++">std::thread my_thread((background_task()));  // 1
std::thread my_thread{background_task()};    // 2
</code></pre>
<p>Lambda&#x8868;&#x8FBE;&#x5F0F;&#x4E5F;&#x80FD;&#x907F;&#x514D;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x662F;C++11&#x7684;&#x4E00;&#x4E2A;&#x65B0;&#x7279;&#x6027;&#xFF0C;&#x5141;&#x8BB8;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x6355;&#x83B7;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x5C40;&#x90E8;&#x51FD;&#x6570;(&#x53EF;&#x4EE5;&#x907F;&#x514D;&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF0C;&#x53C2;&#x89C1;2.2&#x8282;)&#x3002;&#x60F3;&#x8981;&#x8BE6;&#x7EC6;&#x4E86;&#x89E3;Lambda&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x9605;&#x8BFB;&#x9644;&#x5F55;A&#x7684;A.5&#x8282;&#x3002;&#x4E4B;&#x524D;&#x7684;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x6539;&#x5199;&#x4E3A;Lambda&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-built_in">std</span>::<span class="hljs-function">thread <span class="hljs-title">my_thread</span><span class="hljs-params">([]{
  do_something()</span></span>;
  do_something_else();
});
</code></pre>
<p>&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x540E;&#x662F;&#x8981;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF0C;&#x8FD8;&#x662F;&#x8BA9;&#x5176;&#x81EA;&#x4E3B;&#x8FD0;&#x884C;&#x3002;&#x5F53;<code>std::thread</code>&#x5BF9;&#x8C61;&#x9500;&#x6BC1;&#x4E4B;&#x524D;&#x8FD8;&#x6CA1;&#x6709;&#x505A;&#x51FA;&#x51B3;&#x5B9A;&#xFF0C;&#x7A0B;&#x5E8F;&#x5C31;&#x4F1A;&#x7EC8;&#x6B62;(<code>std::thread</code>&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4F1A;&#x8C03;&#x7528;<code>std::terminate()</code>)&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x5373;&#x4FBF;&#x662F;&#x6709;&#x5F02;&#x5E38;&#x5B58;&#x5728;&#xFF0C;&#x4E5F;&#x9700;&#x8981;&#x786E;&#x4FDD;&#x7EBF;&#x7A0B;&#x80FD;&#x591F;&#x6B63;&#x786E;<em>&#x6C47;&#x5165;</em>(joined)&#x6216;<em>&#x5206;&#x79BB;</em>(detached)&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E0D;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x6C47;&#x5165; &#xFF0C;&#x5C31;&#x5FC5;&#x987B;&#x4FDD;&#x8BC1;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#x4E4B;&#x524D;&#xFF0C;&#x8BBF;&#x95EE;&#x6570;&#x636E;&#x7684;&#x6709;&#x6548;&#x6027;&#x3002;&#x8FD9;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x65B0;&#x95EE;&#x9898;&#x2014;&#x2014;&#x5355;&#x7EBF;&#x7A0B;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x5BF9;&#x8C61;&#x9500;&#x6BC1;&#x4E4B;&#x540E;&#x518D;&#x53BB;&#x8BBF;&#x95EE;&#xFF0C;&#x4F1A;&#x4EA7;&#x751F;&#x672A;&#x5B9A;&#x4E49;&#x884C;&#x4E3A;&#x2014;&#x2014;&#x4E0D;&#x8FC7;&#xFF0C;&#x7EBF;&#x7A0B;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x589E;&#x52A0;&#x4E86;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x53D1;&#x751F;&#x7684;&#x51E0;&#x7387;&#x3002;</p>
<p>&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x5F88;&#x53EF;&#x80FD;&#x53D1;&#x751F;&#x5728;&#x7EBF;&#x7A0B;&#x8FD8;&#x6CA1;&#x7ED3;&#x675F;&#xFF0C;&#x51FD;&#x6570;&#x5DF2;&#x7ECF;&#x9000;&#x51FA;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD9;&#x65F6;&#x7EBF;&#x7A0B;&#x51FD;&#x6570;&#x8FD8;&#x6301;&#x6709;&#x51FD;&#x6570;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x6307;&#x9488;&#x6216;&#x5F15;&#x7528;&#x3002;</p>
<p>&#x4EE3;&#x7801;2.1  &#x51FD;&#x6570;&#x5DF2;&#x7ECF;&#x8FD4;&#x56DE;&#xFF0C;&#x7EBF;&#x7A0B;&#x4F9D;&#x65E7;&#x8BBF;&#x95EE;&#x5C40;&#x90E8;&#x53D8;&#x91CF;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">struct</span> func
{
  <span class="hljs-keyword">int</span>&amp; i;
  func(<span class="hljs-keyword">int</span>&amp; i_) : i(i_) {}
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">operator</span><span class="hljs-params">()</span> <span class="hljs-params">()</span>
  </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">unsigned</span> j=<span class="hljs-number">0</span> ; j&lt;<span class="hljs-number">1000000</span> ; ++j)
    {
      do_something(i);           <span class="hljs-comment">// 1 &#x6F5C;&#x5728;&#x8BBF;&#x95EE;&#x9690;&#x60A3;&#xFF1A;&#x7A7A;&#x5F15;&#x7528;</span>
    }
  }
};

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">oops</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-keyword">int</span> some_local_state=<span class="hljs-number">0</span>;
  <span class="hljs-function">func <span class="hljs-title">my_func</span><span class="hljs-params">(some_local_state)</span></span>;
  <span class="hljs-built_in">std</span>::<span class="hljs-function">thread <span class="hljs-title">my_thread</span><span class="hljs-params">(my_func)</span></span>;
  my_thread.detach();          <span class="hljs-comment">// 2 &#x4E0D;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;</span>
}                              <span class="hljs-comment">// 3 &#x65B0;&#x7EBF;&#x7A0B;&#x53EF;&#x80FD;&#x8FD8;&#x5728;&#x8FD0;&#x884C;</span>
</code></pre>
<p>&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x5DF2;&#x7ECF;&#x51B3;&#x5B9A;&#x4E0D;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;(&#x4F7F;&#x7528;&#x4E86;detach()&#x2461;)&#xFF0C;&#x6240;&#x4EE5;&#x5F53;oops()&#x51FD;&#x6570;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x65F6;&#x2462;&#xFF0C;&#x7EBF;&#x7A0B;&#x4E2D;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x80FD;&#x8FD8;&#x5728;&#x8FD0;&#x884C;&#x3002;&#x5982;&#x679C;&#x7EBF;&#x7A0B;&#x8FD8;&#x5728;&#x8FD0;&#x884C;&#xFF0C;&#x5C31;&#x4F1A;&#x53BB;&#x8C03;&#x7528;do_something(i)&#x2460;&#xFF0C;&#x8FD9;&#x65F6;&#x5C31;&#x4F1A;&#x8BBF;&#x95EE;&#x5DF2;&#x7ECF;&#x9500;&#x6BC1;&#x7684;&#x53D8;&#x91CF;&#x3002;&#x5982;&#x540C;&#x4E00;&#x4E2A;&#x5355;&#x7EBF;&#x7A0B;&#x7A0B;&#x5E8F;&#x2014;&#x2014;&#x5141;&#x8BB8;&#x5728;&#x51FD;&#x6570;&#x5B8C;&#x6210;&#x540E;&#x7EE7;&#x7EED;&#x6301;&#x6709;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x6307;&#x9488;&#x6216;&#x5F15;&#x7528;&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x53D1;&#x751F;&#x65F6;&#xFF0C;&#x9519;&#x8BEF;&#x5E76;&#x4E0D;&#x660E;&#x663E;&#xFF0C;&#x4F1A;&#x4F7F;&#x591A;&#x7EBF;&#x7A0B;&#x66F4;&#x5BB9;&#x6613;&#x51FA;&#x9519;&#x3002;&#x8FD0;&#x884C;&#x987A;&#x5E8F;&#x53C2;&#x8003;&#x8868;2.1&#x3002;</p>
<p>&#x8868;2.1 &#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#x5728;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x9500;&#x6BC1;&#x540E;&#xFF0C;&#x4ECD;&#x5BF9;&#x8BE5;&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x8BBF;&#x95EE;</p>
<table>
<thead>
<tr>
<th style="text-align:center">&#x4E3B;&#x7EBF;&#x7A0B;</th>
<th style="text-align:center">&#x65B0;&#x7EBF;&#x7A0B;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">&#x4F7F;&#x7528;some_local_state&#x6784;&#x9020;my_func</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center">&#x5F00;&#x542F;&#x65B0;&#x7EBF;&#x7A0B;my_thread</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center">&#x542F;&#x52A8;</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center">&#x8C03;&#x7528;func::operator()</td>
</tr>
<tr>
<td style="text-align:center">&#x5C06;my_thread&#x5206;&#x79BB;</td>
<td style="text-align:center">&#x6267;&#x884C;func::operator();&#x53EF;&#x80FD;&#x4F1A;&#x5728;do_something&#x4E2D;&#x8C03;&#x7528;some_local_state&#x7684;&#x5F15;&#x7528;</td>
</tr>
<tr>
<td style="text-align:center">&#x9500;&#x6BC1;some_local_state</td>
<td style="text-align:center">&#x6301;&#x7EED;&#x8FD0;&#x884C;</td>
</tr>
<tr>
<td style="text-align:center">&#x9000;&#x51FA;oops&#x51FD;&#x6570;</td>
<td style="text-align:center">&#x6301;&#x7EED;&#x6267;&#x884C;func::operator()&#xFF1B;&#x53EF;&#x80FD;&#x4F1A;&#x5728;do_something&#x4E2D;&#x8C03;&#x7528;some_local_state&#x7684;&#x5F15;&#x7528; --&gt; &#x5BFC;&#x81F4;&#x672A;&#x5B9A;&#x4E49;&#x884C;&#x4E3A;</td>
</tr>
</tbody>
</table>
<p>&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x7684;&#x5E38;&#x89C4;&#x5904;&#x7406;&#x65B9;&#x6CD5;&#xFF1A;&#x5C06;&#x6570;&#x636E;&#x590D;&#x5236;&#x5230;&#x7EBF;&#x7A0B;&#x4E2D;&#x3002;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x53EF;&#x8C03;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x7EBF;&#x7A0B;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5C31;&#x4F1A;&#x590D;&#x5236;&#x5230;&#x7EBF;&#x7A0B;&#x4E2D;&#xFF0C;&#x800C;&#x540E;&#x539F;&#x59CB;&#x5BF9;&#x8C61;&#x4F1A;&#x7ACB;&#x5373;&#x9500;&#x6BC1;&#x3002;&#x5982;&#x4EE3;&#x7801;2.1&#x6240;&#x793A;&#xFF0C;&#x4F46;&#x5BF9;&#x4E8E;&#x5BF9;&#x8C61;&#x4E2D;&#x5305;&#x542B;&#x7684;&#x6307;&#x9488;&#x548C;&#x5F15;&#x7528;&#x8FD8;&#x9700;&#x8C28;&#x614E;&#x3002;&#x4F7F;&#x7528;&#x8BBF;&#x95EE;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x51FD;&#x6570;&#x53BB;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#x662F;&#x4E00;&#x4E2A;&#x7CDF;&#x7CD5;&#x7684;&#x4E3B;&#x610F;&#x3002;</p>
<p>&#x6B64;&#x5916;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;join()&#x51FD;&#x6570;&#x6765;&#x786E;&#x4FDD;&#x7EBF;&#x7A0B;&#x5728;&#x4E3B;&#x51FD;&#x6570;&#x5B8C;&#x6210;&#x524D;&#x7ED3;&#x675F;&#x3002;</p>
<h2 id="212-&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;">2.1.2 &#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;</h2>
<p>&#x5982;&#x9700;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;join()&#x3002;&#x5C06;&#x4EE3;&#x7801;2.1&#x4E2D;&#x7684;<code>my_thread.detach()</code>&#x66FF;&#x6362;&#x4E3A;<code>my_thread.join()</code>&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x786E;&#x4FDD;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x5728;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x540E;&#x624D;&#x9500;&#x6BC1;&#x3002;&#x56E0;&#x4E3A;&#x4E3B;&#x7EBF;&#x7A0B;&#x5E76;&#x6CA1;&#x6709;&#x505A;&#x4EC0;&#x4E48;&#x4E8B;&#xFF0C;&#x4F7F;&#x7528;&#x72EC;&#x7ACB;&#x7684;&#x7EBF;&#x7A0B;&#x53BB;&#x6267;&#x884C;&#x51FD;&#x6570;&#x53D8;&#x5F97;&#x610F;&#x4E49;&#x4E0D;&#x5927;&#x3002;&#x4F46;&#x5728;&#x5B9E;&#x9645;&#x4E2D;&#xFF0C;&#x539F;&#x59CB;&#x7EBF;&#x7A0B;&#x8981;&#x4E48;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x5DE5;&#x4F5C;&#x8981;&#x505A;&#xFF0C;&#x8981;&#x4E48;&#x4F1A;&#x542F;&#x52A8;&#x591A;&#x4E2A;&#x5B50;&#x7EBF;&#x7A0B;&#x6765;&#x505A;&#x4E00;&#x4E9B;&#x6709;&#x7528;&#x7684;&#x5DE5;&#x4F5C;&#xFF0C;&#x5E76;&#x7B49;&#x5F85;&#x8FD9;&#x4E9B;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#x3002;</p>
<p>&#x5F53;&#x4F60;&#x9700;&#x8981;&#x5BF9;&#x7B49;&#x5F85;&#x4E2D;&#x7684;&#x7EBF;&#x7A0B;&#x6709;&#x66F4;&#x7075;&#x6D3B;&#x7684;&#x63A7;&#x5236;&#x65F6;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;&#x770B;&#x4E00;&#x4E0B;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x7ED3;&#x675F;&#xFF0C;&#x6216;&#x8005;&#x53EA;&#x7B49;&#x5F85;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;(&#x8D85;&#x8FC7;&#x65F6;&#x95F4;&#x5C31;&#x5224;&#x5B9A;&#x4E3A;&#x8D85;&#x65F6;)&#x3002;&#x60F3;&#x8981;&#x505A;&#x5230;&#x8FD9;&#x4E9B;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5176;&#x4ED6;&#x673A;&#x5236;&#x6765;&#x5B8C;&#x6210;&#xFF0C;&#x6BD4;&#x5982;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x548C;future&#x3002;&#x8C03;&#x7528;join()&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x6E05;&#x7406;&#x4E86;&#x7EBF;&#x7A0B;&#x76F8;&#x5173;&#x7684;&#x5185;&#x5B58;&#xFF0C;&#x8FD9;&#x6837;<code>std::thread</code>&#x5BF9;&#x8C61;&#x5C06;&#x4E0D;&#x518D;&#x4E0E;&#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#x7684;&#x7EBF;&#x7A0B;&#x6709;&#x4EFB;&#x4F55;&#x5173;&#x8054;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x53EA;&#x80FD;&#x5BF9;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4F7F;&#x7528;&#x4E00;&#x6B21;join()&#xFF0C;&#x4E00;&#x65E6;&#x4F7F;&#x7528;&#x8FC7;join()&#xFF0C;<code>std::thread</code>&#x5BF9;&#x8C61;&#x5C31;&#x4E0D;&#x80FD;&#x518D;&#x6B21;&#x6C47;&#x5165;&#x4E86;&#x3002;&#x5F53;&#x5BF9;&#x5176;&#x4F7F;&#x7528;joinable()&#x65F6;&#xFF0C;&#x5C06;&#x8FD4;&#x56DE;false&#x3002;</p>
<h2 id="213-&#x7279;&#x6B8A;&#x60C5;&#x51B5;&#x4E0B;&#x7684;&#x7B49;&#x5F85;">2.1.3 &#x7279;&#x6B8A;&#x60C5;&#x51B5;&#x4E0B;&#x7684;&#x7B49;&#x5F85;</h2>
<p>&#x5982;&#x524D;&#x6240;&#x8FF0;&#xFF0C;&#x9700;&#x8981;&#x5BF9;&#x4E00;&#x4E2A;&#x672A;&#x9500;&#x6BC1;&#x7684;<code>std::thread</code>&#x5BF9;&#x8C61;&#x4F7F;&#x7528;join()&#x6216;detach()&#x3002;&#x5982;&#x679C;&#x60F3;&#x8981;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x7EBF;&#x7A0B;&#x542F;&#x52A8;&#x540E;&#xFF0C;&#x76F4;&#x63A5;&#x4F7F;&#x7528;detach()&#x8FDB;&#x884C;&#x5206;&#x79BB;&#x3002;&#x5982;&#x679C;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x7EC6;&#x5FC3;&#x6311;&#x9009;&#x4F7F;&#x7528;join()&#x7684;&#x4F4D;&#x7F6E;&#x3002;&#x5F53;&#x5728;&#x7EBF;&#x7A0B;&#x8FD0;&#x884C;&#x540E;&#x4EA7;&#x751F;&#x7684;&#x5F02;&#x5E38;&#xFF0C;&#x4F1A;&#x5728;join()&#x8C03;&#x7528;&#x4E4B;&#x524D;&#x629B;&#x51FA;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x4F1A;&#x8DF3;&#x8FC7;join()&#x3002;</p>
<p>&#x907F;&#x514D;&#x5E94;&#x7528;&#x88AB;&#x629B;&#x51FA;&#x7684;&#x5F02;&#x5E38;&#x6240;&#x7EC8;&#x6B62;&#x3002;&#x901A;&#x5E38;&#xFF0C;&#x5728;&#x65E0;&#x5F02;&#x5E38;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x4F7F;&#x7528;join()&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x8FC7;&#x7A0B;&#x4E2D;&#x8C03;&#x7528;join()&#xFF0C;&#x4ECE;&#x800C;&#x907F;&#x514D;&#x751F;&#x547D;&#x5468;&#x671F;&#x7684;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x4EE3;&#x7801;2.2 &#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">struct</span> func; <span class="hljs-comment">// &#x5B9A;&#x4E49;&#x5728;&#x4EE3;&#x7801;2.1&#x4E2D;</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">f</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-keyword">int</span> some_local_state=<span class="hljs-number">0</span>;
  <span class="hljs-function">func <span class="hljs-title">my_func</span><span class="hljs-params">(some_local_state)</span></span>;
  <span class="hljs-built_in">std</span>::<span class="hljs-function">thread <span class="hljs-title">t</span><span class="hljs-params">(my_func)</span></span>;
  <span class="hljs-keyword">try</span>
  {
    do_something_in_current_thread();
  }
  <span class="hljs-keyword">catch</span>(...)
  {
    t.join();  <span class="hljs-comment">// 1</span>
    <span class="hljs-keyword">throw</span>;
  }
  t.join();  <span class="hljs-comment">// 2</span>
}
</code></pre>
<p>&#x4EE3;&#x7801;2.2&#x4E2D;&#x4F7F;&#x7528;&#x4E86;<code>try/catch</code>&#x5757;&#x786E;&#x4FDD;&#x7EBF;&#x7A0B;&#x9000;&#x51FA;&#x540E;&#x51FD;&#x6570;&#x624D;&#x7ED3;&#x675F;&#x3002;&#x5F53;&#x51FD;&#x6570;&#x6B63;&#x5E38;&#x9000;&#x51FA;&#x540E;&#xFF0C;&#x4F1A;&#x6267;&#x884C;&#x5230;&#x2461;&#x5904;&#x3002;&#x5F53;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x7A0B;&#x5E8F;&#x4F1A;&#x6267;&#x884C;&#x5230;&#x2460;&#x5904;&#x3002;&#x5982;&#x679C;&#x7EBF;&#x7A0B;&#x5728;&#x51FD;&#x6570;&#x4E4B;&#x524D;&#x7ED3;&#x675F;&#x2014;&#x2014;&#x5C31;&#x8981;&#x67E5;&#x770B;&#x662F;&#x5426;&#x56E0;&#x4E3A;&#x7EBF;&#x7A0B;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#x4E86;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x5F15;&#x7528;&#x2014;&#x2014;&#x800C;&#x540E;&#x518D;&#x786E;&#x5B9A;&#x4E00;&#x4E0B;&#x7A0B;&#x5E8F;&#x53EF;&#x80FD;&#x4F1A;&#x9000;&#x51FA;&#x7684;&#x9014;&#x5F84;&#xFF0C;&#x65E0;&#x8BBA;&#x6B63;&#x5E38;&#x4E0E;&#x5426;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x673A;&#x5236;&#xFF0C;&#x53EF;&#x4EE5;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528;&#x201C;&#x8D44;&#x6E90;&#x83B7;&#x53D6;&#x5373;&#x521D;&#x59CB;&#x5316;&#x65B9;&#x5F0F;&#x201D;(RAII&#xFF0C;Resource Acquisition Is Initialization)&#xFF0C;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x5728;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528;join()&#x3002;&#x5982;&#x540C;&#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x4EE3;&#x7801;2.3 &#x4F7F;&#x7528;RAII&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;</p>
<pre><code class="lang-c++">class thread_guard
{
  std::thread&amp; t;
public:
  explicit thread_guard(std::thread&amp; t_):
    t(t_)
  {}
  ~thread_guard()
  {
    if(t.joinable()) // 1
    {
      t.join();      // 2
    }
  }
  thread_guard(thread_guard const&amp;)=delete;   // 3
  thread_guard&amp; operator=(thread_guard const&amp;)=delete;
};

struct func; // &#x5B9A;&#x4E49;&#x5728;&#x4EE3;&#x7801;2.1&#x4E2D;

void f()
{
  int some_local_state=0;
  func my_func(some_local_state);
  std::thread t(my_func);
  thread_guard g(t);
  do_something_in_current_thread();
}    // 4
</code></pre>
<p>&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5230;&#x2463;&#x5904;&#x65F6;&#xFF0C;&#x5C40;&#x90E8;&#x5BF9;&#x8C61;&#x5C31;&#x8981;&#x88AB;&#x9006;&#x5E8F;&#x9500;&#x6BC1;&#x4E86;&#x3002;&#x56E0;&#x6B64;&#xFF0C;thread_guard&#x5BF9;&#x8C61;g&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x88AB;&#x9500;&#x6BC1;&#x7684;&#xFF0C;&#x8FD9;&#x65F6;&#x7EBF;&#x7A0B;&#x5728;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4E2D;&#x88AB;&#x52A0;&#x5165;&#x2461;&#x5230;&#x539F;&#x59CB;&#x7EBF;&#x7A0B;&#x4E2D;&#x3002;&#x5373;&#x4F7F;do_something_in_current_thread&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#xFF0C;&#x8FD9;&#x4E2A;&#x9500;&#x6BC1;&#x4F9D;&#x65E7;&#x4F1A;&#x53D1;&#x751F;&#x3002;</p>
<p>&#x5728;thread_guard&#x6790;&#x6784;&#x51FD;&#x6570;&#x7684;&#x6D4B;&#x8BD5;&#x4E2D;&#xFF0C;&#x9996;&#x5148;&#x5224;&#x65AD;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x53EF;&#x6C47;&#x5165;&#x2460;&#x3002;&#x5982;&#x679C;&#x53EF;&#x6C47;&#x5165;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;join()&#x2461;&#x8FDB;&#x884C;&#x6C47;&#x5165;&#x3002;</p>
<p>&#x62F7;&#x8D1D;&#x6784;&#x9020;&#x51FD;&#x6570;&#x548C;&#x62F7;&#x8D1D;&#x8D4B;&#x503C;&#x64CD;&#x4F5C;&#x6807;&#x8BB0;&#x4E3A;<code>=delete</code>&#x2462;&#xFF0C;&#x662F;&#x4E3A;&#x4E86;&#x4E0D;&#x8BA9;&#x7F16;&#x8BD1;&#x5668;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x3002;&#x76F4;&#x63A5;&#x5BF9;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x62F7;&#x8D1D;&#x6216;&#x8D4B;&#x503C;&#x662F;&#x5F88;&#x5371;&#x9669;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x53EF;&#x80FD;&#x4F1A;&#x5F04;&#x4E22;&#x5DF2;&#x6C47;&#x5165;&#x7684;&#x7EBF;&#x7A0B;&#x3002;&#x901A;&#x8FC7;&#x5220;&#x9664;&#x58F0;&#x660E;&#xFF0C;&#x4EFB;&#x4F55;&#x5C1D;&#x8BD5;&#x7ED9;thread_guard&#x5BF9;&#x8C61;&#x8D4B;&#x503C;&#x7684;&#x64CD;&#x4F5C;&#x90FD;&#x4F1A;&#x5F15;&#x53D1;&#x4E00;&#x4E2A;&#x7F16;&#x8BD1;&#x9519;&#x8BEF;&#x3002;&#x60F3;&#x8981;&#x4E86;&#x89E3;&#x5220;&#x9664;&#x51FD;&#x6570;&#x7684;&#x66F4;&#x591A;&#x77E5;&#x8BC6;&#xFF0C;&#x8BF7;&#x53C2;&#x9605;&#x9644;&#x5F55;A&#x7684;A.2&#x8282;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E0D;&#x60F3;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF0C;&#x53EF;&#x4EE5;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#xFF0C;&#x4ECE;&#x800C;&#x907F;&#x514D;&#x5F02;&#x5E38;&#x3002;&#x4E0D;&#x8FC7;&#xFF0C;&#x8FD9;&#x5C31;&#x6253;&#x7834;&#x4E86;&#x7EBF;&#x7A0B;&#x4E0E;<code>std::thread</code>&#x5BF9;&#x8C61;&#x7684;&#x8054;&#x7CFB;&#xFF0C;&#x5373;&#x4F7F;&#x7EBF;&#x7A0B;&#x4ECD;&#x7136;&#x5728;&#x540E;&#x53F0;&#x8FD0;&#x884C;&#x7740;&#xFF0C;&#x5206;&#x79BB;&#x64CD;&#x4F5C;&#x4E5F;&#x80FD;&#x786E;&#x4FDD;&#x5728;<code>std::thread</code>&#x5BF9;&#x8C61;&#x9500;&#x6BC1;&#x65F6;&#x4E0D;&#x8C03;&#x7528;<code>std::terminate()</code>&#x3002;</p>
<h2 id="214-&#x540E;&#x53F0;&#x8FD0;&#x884C;&#x7EBF;&#x7A0B;">2.1.4 &#x540E;&#x53F0;&#x8FD0;&#x884C;&#x7EBF;&#x7A0B;</h2>
<p>&#x4F7F;&#x7528;detach()&#x4F1A;&#x8BA9;&#x7EBF;&#x7A0B;&#x5728;&#x540E;&#x53F0;&#x8FD0;&#x884C;&#xFF0C;&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;&#x4E0E;&#x4E3B;&#x7EBF;&#x7A0B;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x4EA4;&#x4E92;&#x3002;&#x5982;&#x679C;&#x7EBF;&#x7A0B;&#x5206;&#x79BB;&#xFF0C;&#x5C31;&#x4E0D;&#x53EF;&#x80FD;&#x6709;<code>std::thread</code>&#x5BF9;&#x8C61;&#x80FD;&#x5F15;&#x7528;&#x5B83;&#xFF0C;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#x7684;&#x786E;&#x5728;&#x540E;&#x53F0;&#x8FD0;&#x884C;&#xFF0C;&#x6240;&#x4EE5;&#x5206;&#x79BB;&#x7684;&#x7EBF;&#x7A0B;&#x4E0D;&#x80FD;&#x6C47;&#x5165;&#x3002;&#x4E0D;&#x8FC7;C++&#x8FD0;&#x884C;&#x5E93;&#x4FDD;&#x8BC1;&#xFF0C;&#x5F53;&#x7EBF;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#xFF0C;&#x76F8;&#x5173;&#x8D44;&#x6E90;&#x7684;&#x80FD;&#x591F;&#x6B63;&#x786E;&#x56DE;&#x6536;&#x3002;</p>
<p>&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#x901A;&#x5E38;&#x79F0;&#x4E3A;<em>&#x5B88;&#x62A4;&#x7EBF;&#x7A0B;</em>(daemon threads)&#x3002;UNIX&#x4E2D;&#x5B88;&#x62A4;&#x7EBF;&#x7A0B;&#xFF0C;&#x662F;&#x6307;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x663E;&#x5F0F;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x5E76;&#x5728;&#x540E;&#x53F0;&#x8FD0;&#x884C;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x8FD9;&#x79CD;&#x7EBF;&#x7A0B;&#x7684;&#x7279;&#x70B9;&#x5C31;&#x662F;&#x957F;&#x65F6;&#x95F4;&#x8FD0;&#x884C;&#x3002;&#x7EBF;&#x7A0B;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x53EF;&#x80FD;&#x4F1A;&#x4ECE;&#x5E94;&#x7528;&#x7684;&#x8D77;&#x59CB;&#x5230;&#x7ED3;&#x675F;&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x5728;&#x540E;&#x53F0;&#x76D1;&#x89C6;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#xFF0C;&#x8FD8;&#x6709;&#x53EF;&#x80FD;&#x5BF9;&#x7F13;&#x5B58;&#x8FDB;&#x884C;&#x6E05;&#x7406;&#xFF0C;&#x4EA6;&#x6216;&#x5BF9;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x8FDB;&#x884C;&#x4F18;&#x5316;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#x53EA;&#x80FD;&#x786E;&#x5B9A;&#x7EBF;&#x7A0B;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x7ED3;&#x675F;&#xFF0C;<em>&#x53D1;&#x540E;&#x5373;&#x5FD8;</em>(fire and forget)&#x7684;&#x4EFB;&#x52A1;&#x4F7F;&#x7528;&#x5230;&#x5C31;&#x662F;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#x3002;</p>
<p>&#x5982;2.1.2&#x8282;&#x6240;&#x793A;&#xFF0C;&#x8C03;&#x7528;<code>std::thread</code>&#x6210;&#x5458;&#x51FD;&#x6570;detach()&#x6765;&#x5206;&#x79BB;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x3002;&#x4E4B;&#x540E;&#xFF0C;&#x76F8;&#x5E94;&#x7684;<code>std::thread</code>&#x5BF9;&#x8C61;&#x5C31;&#x4E0E;&#x5B9E;&#x9645;&#x6267;&#x884C;&#x7684;&#x7EBF;&#x7A0B;&#x65E0;&#x5173;&#x4E86;&#xFF0C;&#x5E76;&#x4E14;&#x8FD9;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E5F;&#x65E0;&#x6CD5;&#x6C47;&#x5165;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-built_in">std</span>::<span class="hljs-function">thread <span class="hljs-title">t</span><span class="hljs-params">(do_background_work)</span></span>;
t.detach();
assert(!t.joinable());
</code></pre>
<p>&#x4E3A;&#x4E86;&#x4ECE;<code>std::thread</code>&#x5BF9;&#x8C61;&#x4E2D;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#xFF0C;&#x4E0D;&#x80FD;&#x5BF9;&#x6CA1;&#x6709;&#x6267;&#x884C;&#x7EBF;&#x7A0B;&#x7684;<code>std::thread</code>&#x5BF9;&#x8C61;&#x4F7F;&#x7528;detach()&#xFF0C;&#x5E76;&#x4E14;&#x8981;&#x7528;&#x540C;&#x6837;&#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#x2014;&#x2014;&#x5F53;<code>std::thread</code>&#x5BF9;&#x8C61;&#x4F7F;&#x7528;t.joinable()&#x8FD4;&#x56DE;&#x7684;&#x662F;true&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;t.detach()&#x3002;</p>
<p>&#x8BD5;&#x60F3;&#x5982;&#x4F55;&#x80FD;&#x8BA9;&#x4E00;&#x4E2A;&#x6587;&#x5B57;&#x5904;&#x7406;&#x5E94;&#x7528;&#x540C;&#x65F6;&#x7F16;&#x8F91;&#x591A;&#x4E2A;&#x6587;&#x6863;&#x3002;&#x65E0;&#x8BBA;&#x662F;&#x7528;&#x6237;&#x754C;&#x9762;&#xFF0C;&#x8FD8;&#x662F;&#x5728;&#x5185;&#x90E8;&#x5E94;&#x7528;&#x5185;&#x90E8;&#x8FDB;&#x884C;&#xFF0C;&#x90FD;&#x6709;&#x5F88;&#x591A;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#x3002;&#x867D;&#x7136;&#xFF0C;&#x8FD9;&#x4E9B;&#x7A97;&#x53E3;&#x770B;&#x8D77;&#x6765;&#x662F;&#x5B8C;&#x5168;&#x72EC;&#x7ACB;&#x7684;&#xFF0C;&#x6BCF;&#x4E2A;&#x7A97;&#x53E3;&#x90FD;&#x6709;&#x81EA;&#x5DF1;&#x72EC;&#x7ACB;&#x7684;&#x83DC;&#x5355;&#x9009;&#x9879;&#xFF0C;&#x4F46;&#x4ED6;&#x4EEC;&#x5374;&#x8FD0;&#x884C;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x5B9E;&#x4F8B;&#x4E2D;&#x3002;&#x4E00;&#x79CD;&#x5185;&#x90E8;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x662F;&#xFF0C;&#x8BA9;&#x6BCF;&#x4E2A;&#x6587;&#x6863;&#x5904;&#x7406;&#x7A97;&#x53E3;&#x62E5;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x7EBF;&#x7A0B;&#x3002;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x8FD0;&#x884C;&#x540C;&#x6837;&#x7684;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5E76;&#x9694;&#x79BB;&#x4E0D;&#x540C;&#x7A97;&#x53E3;&#x5904;&#x7406;&#x7684;&#x6570;&#x636E;&#x3002;&#x5982;&#x6B64;&#x8FD9;&#x822C;&#xFF0C;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x5C31;&#x8981;&#x542F;&#x52A8;&#x4E00;&#x4E2A;&#x65B0;&#x7EBF;&#x7A0B;&#x3002;&#x56E0;&#x4E3A;&#x662F;&#x5BF9;&#x72EC;&#x7ACB;&#x6587;&#x6863;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;&#x6240;&#x4EE5;&#x6CA1;&#x6709;&#x5FC5;&#x8981;&#x7B49;&#x5F85;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#xFF0C;&#x8FD9;&#x91CC;&#x5C31;&#x53EF;&#x4EE5;&#x8BA9;&#x6587;&#x6863;&#x5904;&#x7406;&#x7A97;&#x53E3;&#x8FD0;&#x884C;&#x5728;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#x4E0A;&#x3002;</p>
<p>&#x4EE3;&#x7801;2.4 &#x4F7F;&#x7528;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#x5904;&#x7406;&#x6587;&#x6863;</p>
<pre><code class="lang-c++"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">edit_document</span><span class="hljs-params">(<span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> <span class="hljs-keyword">const</span>&amp; filename)</span>
</span>{
  open_document_and_display_gui(filename);
  <span class="hljs-keyword">while</span>(!done_editing())
  {
    user_command cmd=get_user_input();
    <span class="hljs-keyword">if</span>(cmd.type==open_new_document)
    {
      <span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> <span class="hljs-keyword">const</span> new_name=get_filename_from_user();
      <span class="hljs-built_in">std</span>::<span class="hljs-function">thread <span class="hljs-title">t</span><span class="hljs-params">(edit_document,new_name)</span></span>;  <span class="hljs-comment">// 1</span>
      t.detach();  <span class="hljs-comment">// 2</span>
    }
    <span class="hljs-keyword">else</span>
    {
       process_user_input(cmd);
    }
  }
}
</code></pre>
<p>&#x5982;&#x679C;&#x7528;&#x6237;&#x9009;&#x62E9;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x65B0;&#x6587;&#x6863;&#xFF0C;&#x9700;&#x8981;&#x542F;&#x52A8;&#x4E00;&#x4E2A;&#x65B0;&#x7EBF;&#x7A0B;&#x53BB;&#x6253;&#x5F00;&#x65B0;&#x6587;&#x6863;&#x2460;&#xFF0C;&#x5E76;&#x5206;&#x79BB;&#x7EBF;&#x7A0B;&#x2461;&#x3002;&#x4E0E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x505A;&#x51FA;&#x7684;&#x64CD;&#x4F5C;&#x4E00;&#x6837;&#xFF0C;&#x65B0;&#x7EBF;&#x7A0B;&#x53EA;&#x4E0D;&#x8FC7;&#x662F;&#x6253;&#x5F00;&#x53E6;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x800C;&#x5DF2;&#x3002;&#x6240;&#x4EE5;&#xFF0C;edit_document&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x590D;&#x7528;&#xFF0C; &#x5E76;&#x901A;&#x8FC7;&#x4F20;&#x53C2;&#x7684;&#x5F62;&#x5F0F;&#x6253;&#x5F00;&#x65B0;&#x7684;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E5F;&#x5C55;&#x793A;&#x4E86;&#x4F20;&#x53C2;&#x542F;&#x52A8;&#x7EBF;&#x7A0B;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;&#x4E0D;&#x4EC5;&#x53EF;&#x4EE5;&#x5411;<code>std::thread</code>&#x6784;&#x9020;&#x51FD;&#x6570;&#x2460;&#x4F20;&#x9012;&#x51FD;&#x6570;&#x540D;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F20;&#x9012;&#x51FD;&#x6570;&#x6240;&#x9700;&#x7684;&#x53C2;&#x6570;(&#x5B9E;&#x53C2;)&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x4E5F;&#x6709;&#x5176;&#x4ED6;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x8FD9;&#x9879;&#x529F;&#x80FD;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;&#x4F7F;&#x7528;&#x5E26;&#x6709;&#x6570;&#x636E;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;&#xFF0C;&#x4EE3;&#x66FF;&#x9700;&#x8981;&#x4F20;&#x53C2;&#x7684;&#x666E;&#x901A;&#x51FD;&#x6570;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="2.0-chinese.html" class="navigation navigation-prev " aria-label="Previous page: 第2章 线程管理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="2.2-chinese.html" class="navigation navigation-next " aria-label="Next page: 2.2 传递参数">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.1 线程的基本操作","level":"1.9.1","depth":2,"next":{"title":"2.2 传递参数","level":"1.9.2","depth":2,"path":"content/chapter2/2.2-chinese.md","ref":"content/chapter2/2.2-chinese.md","articles":[]},"previous":{"title":"第2章 线程管理","level":"1.9","depth":1,"path":"content/chapter2/2.0-chinese.md","ref":"content/chapter2/2.0-chinese.md","articles":[{"title":"2.1 线程的基本操作","level":"1.9.1","depth":2,"path":"content/chapter2/2.1-chinese.md","ref":"content/chapter2/2.1-chinese.md","articles":[]},{"title":"2.2 传递参数","level":"1.9.2","depth":2,"path":"content/chapter2/2.2-chinese.md","ref":"content/chapter2/2.2-chinese.md","articles":[]},{"title":"2.3 转移所有权","level":"1.9.3","depth":2,"path":"content/chapter2/2.3-chinese.md","ref":"content/chapter2/2.3-chinese.md","articles":[]},{"title":"2.4 确定线程数量","level":"1.9.4","depth":2,"path":"content/chapter2/2.4-chinese.md","ref":"content/chapter2/2.4-chinese.md","articles":[]},{"title":"2.5 线程标识","level":"1.9.5","depth":2,"path":"content/chapter2/2.5-chinese.md","ref":"content/chapter2/2.5-chinese.md","articles":[]},{"title":"2.6 本章总结","level":"1.9.6","depth":2,"path":"content/chapter2/2.6-chinese.md","ref":"content/chapter2/2.6-chinese.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"plugins":["katex","ace"],"livereload":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"fontSettings":{"theme":"white","family":"msyh","size":2},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"陈晓伟 chenxiaowei","pdf":{"pageNumbers":false,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":10,"left":10,"top":10,"bottom":20}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"C++并发编程实战（第2版）","language":"zh-hans","gitbook":"*","description":"《C++ Concurrency In Action - SECOND EDITION》英文版的中文翻译版。","direction":"ltr"},"file":{"path":"content/chapter2/2.1-chinese.md","mtime":"2022-11-19T02:54:31.302Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-11-19T07:50:48.148Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

